package com.pfzy.utildemo.activity.record.soundrecord;

import android.text.TextUtils;

import com.pfzy.lib.config.log.MyLog;
import com.pfzy.utildemo.activity.record.soundrecord.db.AudioFileInfo;
import com.pfzy.utildemo.activity.record.util.FileDirNameUtil;

/**
 * 声音文件分段上传
 */
class SoundFileSplitUpload {
    private static SoundFileSplitUpload mSoundFileSplitUpload;
    //    private static int mSplitTimeInterval;
    private String mGroupIdDir;
    private RecordingInfo mRecordingInfo;
    //    private Handler mHandler = new Handler(Looper.getMainLooper());
//    private volatile int mCount = 0;
    private AudioFileInfo mAudioFileInfo = null;
//    private Runnable splitRunnable = new Runnable() {
//        @Override
//        public void run() {
//            splitFile();
//            UpLoadHelper.execUpload(mGroupIdDir, mRecordingInfo);
//            if (RecordHelper.getInstance().isRecording()) {
//                mHandler.removeCallbacks(splitRunnable);
//                mHandler.postDelayed(splitRunnable, SoundFileSplitUpload.mSplitTimeInterval);
//            }
//        }
//    };
//
//    SoundFileSplitUpload() { }

    public static SoundFileSplitUpload getInstance() {
        if (mSoundFileSplitUpload == null) {
            mSoundFileSplitUpload = new SoundFileSplitUpload();
        }
        return mSoundFileSplitUpload;
    }

    /**
     * 设置新录制信息
     *
     * @param recordingInfo
     */
    public void setNewRecordingInfo(RecordingInfo recordingInfo) {
        this.mCount = 0;
        this.mGroupIdDir = null;
        this.mAudioFileInfo = null;
        this.mRecordingInfo = recordingInfo;
        mSplitTimeInterval = this.mRecordingInfo.getSplitTimeIntervalMs();
        if (mSplitTimeInterval < 60000) {
            mSplitTimeInterval = 60000;
        }
    }

    /**
     * 获取新录制文件
     */
    public AudioFileInfo getNewRecordingFile() {
        this.mAudioFileInfo = createFileInfoAndSave2Db();
        return this.mAudioFileInfo;
    }
//
//    /**
//     * 启动拆分任务
//     */
//    public void startSplitTask() {
//        this.mHandler.removeCallbacks(this.splitRunnable);
//        this.mHandler.postDelayed(this.splitRunnable, mSplitTimeInterval);
//    }

    /**
     * 创建文件信息并保存到数据库
     */
    public synchronized AudioFileInfo createFileInfoAndSave2Db() {
        AudioFileInfo fileInfo = null;
        if (this.mRecordingInfo == null) {
            fileInfo = null;
        } else {
            String orderNo = this.mRecordingInfo.getOrderNo();
            if (TextUtils.isEmpty(this.mGroupIdDir)) {
                this.mGroupIdDir = FileDirNameUtil.getPartRecordFileName(orderNo);
            }

            String recordFileName = FileDirNameUtil.getRecordFileName(this.mGroupIdDir, this.mCount, SoundRecordSwitch.isUseAAC());
            fileInfo = new AudioFileInfo();
            fileInfo.setGroupid(this.mGroupIdDir);
            fileInfo.setOrderNo(orderNo);
            fileInfo.setGroupTask(false);
            fileInfo.setUid(this.mRecordingInfo.getUid());
            fileInfo.setUtype(this.mRecordingInfo.getUtype());
            fileInfo.setBizLine(this.mRecordingInfo.getBizLine());
            fileInfo.setRecordStartTime(TimeUtil.currentTimeMillis());
            fileInfo.setRecordStatus(1);
            fileInfo.fileName = recordFileName;
            fileInfo.setExtra(new DbExtraBean(recordFileName).toJsonString());
            fileInfo.setExtraCount(this.mCount);
            fileInfo.setPosition("0");
            fileInfo.setScene("order");
            fileInfo.setRecordType("normal");
            String filePath = FileUtil.createFile(CommonUtil.getContext(), FileDirNameUtil.getRecordFileDir(), recordFileName);
            fileInfo.setAudioFilePath(filePath);
            DbManager.getInstance().insert(fileInfo);
            this.mCount++;
        }
        return fileInfo;
    }

//    /**
//     * 拆分文件
//     */
//    public void splitFile() {
//        if (RecordHelper.getInstance().isRecording()) {
//            Logger.i(TAG, "------------------- splitFile() -> mCount= " + this.mCount);
//            updateFileInfo2RecordDone();
//            this.mAudioFileInfo = createFileInfoAndSave2Db();
//            Logger.i(TAG, "splitFile() -> mAudioFileInfo=" + this.mAudioFileInfo);
//            RecordHelper.getInstance().changeNewRecordPath(this.mAudioFileInfo.getAudioFilePath());
//        }
//    }
//
//    /**
//     * 更新文件信息2记录完成
//     */
//    public void updateFileInfo2RecordDone() {
//        if (this.mAudioFileInfo == null) {
//            Logger.e(TAG, "updateFileInfo2RecoundDone() fileInfo 为null");
//            return;
//        }
//        Logger.i(TAG, "updateFileInfo2RecordDone()");
//        this.mAudioFileInfo.setRecordEndTime(TimeUtil.currentTimeMillis());
//        this.mAudioFileInfo.setRecordStatus(2);
//        DbManager.getInstance().update(this.mAudioFileInfo);
//    }
//
//    public void destroy() {
//        this.mHandler.removeCallbacksAndMessages(null);
//    }

    private void _logd(String msg) {
        MyLog.logd("声音文件分段上传 " + msg);
    }

    private void _loge(String msg) {
        MyLog.loge("声音文件分段上传 " + msg);
    }
}
